<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Db;

class Formulate extends Common
{
	function formulate($val)
	{
		$where = array();
		$order = 'id desc,Sort desc';
		$vm = Db::name($val)->where($where)->order($order)->select();
		$arr = [
			'formulate_list'=>$vm,
			'val'=>$val,
			];
		return view('index',$arr);
	}
	public function deleteFormulate($id,$val)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$where['id'] = ['in',$id];
		if(Db::name($val)->where($where)->delete())
		return json(['code'=>1,'msg'=>'删除成功']);
		return json(['code'=>0,'msg'=>'删除失败']);
	}
	public function toexamineFormulate($id,$sta,$val)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$sta = $sta == 1?2:1;
		if(Db::name($val)->where(array('id'=>$id))->setField('State',$sta))
		return json(['code'=>1,'msg'=>'更新成功']);
		return json(['code'=>0,'msg'=>'更新失败']);
	}
	public function editFormulateInfo($id,$nme,$sto,$val)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$where['id'] = ['<>',$id];
		$where['Name'] = ['=',$nme];
		if(Db::name($val)->where($where)->find())
		return json(['code'=>0,'msg'=>'修改失败，名称已存在']);
		if(Db::name($val)->where(array('id'=>$id))->update(array('Name'=>$nme,'Sort'=>$sto)))
		return json(['code'=>1,'msg'=>'修改成功']);
		return json(['code'=>0,'msg'=>'修改失败']);
	}
	public function addFormulateInfo($nme,$val)
	{
		if (!$this->REQUEST->isAjax()) return "非法操作";
		$where['Name'] = ['=',$nme];
		if(Db::name($val)->where($where)->find())
		return json(['code'=>0,'msg'=>'添加失败，名称已存在']);
		if(Db::name($val)->insert(array('Name'=>$nme)))
		return json(['code'=>1,'msg'=>'添加成功']);
		return json(['code'=>0,'msg'=>'添加失败']);
	}
	function hobby()
	{
		return $this->formulate('hobby');
	}
	function industry()
	{
		return $this->formulate('industry');
	}
	function campus()
	{
		return $this->formulate('campus');
	}
}
